home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / dev / lang / perl_bin.lha / bin / perldoc < prev    next >
Encoding:
Text File  |  1996-08-15  |  8.8 KB  |  358 lines

  1. #!/ade/bin/perl5
  2.     eval 'exec perl -S $0 "$@"'
  3.     if 0;
  4.     eval 'exec perl -S $0 "$@"'
  5.     if 0;
  6.  
  7. #
  8. # Perldoc revision #1 -- look up a piece of documentation in .pod format that
  9. # is embedded in the perl installation tree.
  10. #
  11. # This is not to be confused with Tom Christianson's perlman, which is a
  12. # man replacement, written in perl. This perldoc is strictly for reading
  13. # the perl manuals, though it too is written in perl.
  14.  
  15. if(@ARGV<1) {
  16.     die <<EOF;
  17. Usage: $0 [-h] [-v] [-t] [-u] [-m] PageName|ModuleName|ProgramName
  18.  
  19. We suggest you use "perldoc perldoc" to get aquainted 
  20. with the system.
  21. EOF
  22. }
  23.  
  24. use Getopt::Std;
  25. $Is_VMS = $^O eq 'VMS';
  26.  
  27. sub usage{
  28.         warn "@_\n" if @_;
  29.     die <<EOF;
  30. perldoc [-h] [-v] [-u] PageName|ModuleName|ProgramName...
  31.     -h   Display this help message.
  32.     -t   Display pod using pod2text instead of pod2man and nroff.
  33.     -u     Display unformatted pod text
  34.     -m   Display modules file in its entirety
  35.     -v     Verbosely describe what's going on.
  36. PageName|ModuleName...
  37.          is the name of a piece of documentation that you want to look at. You 
  38.          may either give a descriptive name of the page (as in the case of
  39.          `perlfunc') the name of a module, either like `Term::Info', 
  40.          `Term/Info', the partial name of a module, like `info', or 
  41.          `makemaker', or the name of a program, like `perldoc'.
  42.          
  43. Any switches in the PERLDOC environment variable will be used before the 
  44. command line arguments.
  45.  
  46. EOF
  47. }
  48.  
  49. use Text::ParseWords;
  50.  
  51.  
  52. unshift(@ARGV,shellwords($ENV{"PERLDOC"}));
  53.  
  54. getopts("mhtuv") || usage;
  55.  
  56. usage if $opt_h || $opt_h; # avoid -w warning
  57.  
  58. usage("only one of -t, -u, or -m") if $opt_t + $opt_u + $opt_m > 1;
  59.  
  60. if ($opt_t) { require Pod::Text; import Pod::Text; }
  61.  
  62. @pages = @ARGV;
  63.  
  64. sub containspod {
  65.     my($file) = @_;
  66.     local($_);
  67.     open(TEST,"<$file");
  68.     while(<TEST>) {
  69.         if(/^=head/) {
  70.             close(TEST);
  71.             return 1;
  72.         }
  73.     }
  74.     close(TEST);
  75.     return 0;
  76. }
  77.  
  78.  sub minus_f_nocase {
  79.      my($file) = @_;
  80.      local *DIR;
  81.      local($")="/";
  82.      my(@p,$p,$cip);
  83.      foreach $p (split(/\//, $file)){
  84.     if ($Is_VMS and not scalar @p) {
  85.         # VMS filesystems don't begin at '/'
  86.         push(@p,$p);
  87.         next;
  88.     }
  89.      if (-d ("@p/$p")){
  90.          push @p, $p;
  91.      } elsif (-f ("@p/$p")) {
  92.          return "@p/$p";
  93.      } else {
  94.          my $found=0;
  95.          my $lcp = lc $p;
  96.          opendir DIR, "@p";
  97.          while ($cip=readdir(DIR)) {
  98.         $cip =~ s/\.dir$// if $Is_VMS;
  99.          if (lc $cip eq $lcp){
  100.              $found++;
  101.              last;
  102.          }
  103.          }
  104.          closedir DIR;
  105.          return "" unless $found;
  106.          push @p, $cip;
  107.          return "@p" if -f "@p";
  108.      }
  109.      }
  110.      return; # is not a file
  111.  }
  112.  
  113.   sub searchfor {
  114.       my($recurse,$s,@dirs) = @_;
  115.       $s =~ s!::!/!g;
  116.       $s = VMS::Filespec::unixify($s) if $Is_VMS;
  117.       printf STDERR "looking for $s in @dirs\n" if $opt_v;
  118.      my $ret;
  119.      my $i;
  120.      my $dir;
  121.       for ($i=0;$i<@dirs;$i++) {
  122.           $dir = $dirs[$i];
  123.           ($dir = VMS::Filespec::unixpath($dir)) =~ s!/$!! if $Is_VMS;
  124.          if ((    $ret = minus_f_nocase "$dir/$s.pod")
  125.          or ( $ret = minus_f_nocase "$dir/$s.pm"  and containspod($ret))
  126.          or ( $ret = minus_f_nocase "$dir/$s"     and containspod($ret))
  127.          or ( $Is_VMS and 
  128.               $ret = minus_f_nocase "$dir/$s.com" and containspod($ret))
  129.          or ( $ret = minus_f_nocase "$dir/pod/$s.pod")
  130.          or ( $ret = minus_f_nocase "$dir/pod/$s" and containspod($ret)))
  131.          { return $ret; }
  132.          
  133.          if($recurse) {
  134.             opendir(D,$dir);
  135.             my(@newdirs) = grep(-d,map("$dir/$_",grep(!/^\.\.?$/,readdir(D))));
  136.             closedir(D);
  137.             @newdirs = map((s/.dir$//,$_)[1],@newdirs) if $Is_VMS;
  138.             next unless @newdirs;
  139.             print STDERR "Also looking in @newdirs\n" if $opt_v;
  140.             push(@dirs,@newdirs);
  141.          }
  142.      }
  143.       return ();
  144.   }
  145.  
  146.  
  147. foreach (@pages) {
  148.     print STDERR "Searching for $_\n" if $opt_v;
  149.     # We must look both in @INC for library modules and in PATH
  150.     # for executables, like h2xs or perldoc itself.
  151.     @searchdirs = @INC;
  152.     unless ($opt_m) { 
  153.         if ($Is_VMS) {
  154.         my($i,$trn);
  155.         for ($i = 0; $trn = $ENV{'DCL$PATH'.$i}; $i++) {
  156.             push(@searchdirs,$trn);
  157.         }
  158.         } else {
  159.             push(@searchdirs, grep(-d, split(':', $ENV{'PATH'})));
  160.         }
  161.         @files= searchfor(0,$_,@searchdirs);
  162.     }
  163.     if( @files ) {
  164.         print STDERR "Found as @files\n" if $opt_v;
  165.     } else {
  166.         # no match, try recursive search
  167.         
  168.         @searchdirs = grep(!/^\.$/,@INC);
  169.         
  170.         
  171.         @files= searchfor(1,$_,@searchdirs);
  172.         if( @files ) {
  173.             print STDERR "Loosely found as @files\n" if $opt_v;
  174.         } else {
  175.             print STDERR "No documentation found for '$_'\n";
  176.         }
  177.     }
  178.     push(@found,@files);
  179. }
  180.  
  181. if(!@found) {
  182.     exit ($Is_VMS ? 98962 : 1);
  183. }
  184.  
  185. if( ! -t STDOUT ) { $opt_f = 1 }
  186.  
  187. unless($Is_VMS) {
  188.     $tmp = "/tmp/perldoc1.$$";
  189.     $goodresult = 0;
  190.     @pagers = qw( more less pg view cat );
  191.     unshift(@pagers,$ENV{PAGER}) if $ENV{PAGER};
  192. } else {
  193.     $tmp = 'Sys$Scratch:perldoc.tmp1_'.$$;
  194.     @pagers = qw( most more less type/page );
  195.     unshift(@pagers,$ENV{PERLDOC_PAGER}) if $ENV{PERLDOC_PAGER};
  196.     $goodresult = 1;
  197. }
  198.  
  199. if ($opt_m) {
  200.     foreach $pager (@pagers) {
  201.     my($sts) = system("$pager @found");
  202.     exit 0 if ($Is_VMS ? ($sts & 1) : !$sts);
  203.     }
  204.     exit $Is_VMS ? $sts : 1;
  205.  
  206. foreach (@found) {
  207.  
  208.     if($opt_t) {
  209.         open(TMP,">>$tmp");
  210.         Pod::Text::pod2text($_,*TMP);
  211.         close(TMP);
  212.     } elsif(not $opt_u) {
  213.         open(TMP,">>$tmp");
  214.         $rslt = `pod2man $_ | nroff -man`;
  215.         if ($Is_VMS) { $err = !($? % 2) || $rslt =~ /IVVERB/; }
  216.         else      { $err = $?; }
  217.         print TMP $rslt unless $err;
  218.         close TMP;
  219.     }
  220.                                                     
  221.     if( $opt_u or $err or -z $tmp) {
  222.         open(OUT,">>$tmp");
  223.         open(IN,"<$_");
  224.         $cut = 1;
  225.         while (<IN>) {
  226.             $cut = $1 eq 'cut' if /^=(\w+)/;
  227.             next if $cut;
  228.             print OUT;
  229.         }
  230.         close(IN);
  231.         close(OUT);
  232.     }
  233. }
  234.  
  235. if( $opt_f ) {
  236.     open(TMP,"<$tmp");
  237.     print while <TMP>;
  238.     close(TMP);
  239. } else {
  240.     foreach $pager (@pagers) {
  241.         $sts = system("$pager $tmp");
  242.         last if $Is_VMS && ($sts & 1);
  243.         last unless $sts;
  244.     }
  245. }
  246.  
  247. 1 while unlink($tmp); #Possibly pointless VMSism
  248.  
  249. exit 0;
  250.  
  251. __END__
  252.  
  253. =head1 NAME
  254.  
  255. perldoc - Look up Perl documentation in pod format.
  256.  
  257. =head1 SYNOPSIS
  258.  
  259. B<perldoc> [B<-h>] [B<-v>] [B<-t>] [B<-u>] PageName|ModuleName|ProgramName
  260.  
  261. =head1 DESCRIPTION
  262.  
  263. I<perldoc> looks up a piece of documentation in .pod format that is
  264. embedded in the perl installation tree or in a perl script, and displays
  265. it via pod2man | nroff -man | $PAGER.  This is primarily used for the
  266. documentation for the perl library modules. 
  267.  
  268. Your system may also have man pages installed for those modules, in
  269. which case you can probably just use the man(1) command.
  270.  
  271. =head1 OPTIONS
  272.  
  273. =over 5
  274.  
  275. =item B<-h> help
  276.  
  277. Prints out a brief help message.
  278.  
  279. =item B<-v> verbose
  280.  
  281. Describes search for the item in detail.
  282.  
  283. =item B<-t> text output
  284.  
  285. Display docs using plain text converter, instead of nroff. This may be faster,
  286. but it won't look as nice.
  287.  
  288. =item B<-u> unformatted
  289.  
  290. Find docs only; skip reformatting by pod2*
  291.  
  292. =item B<-m> module
  293.  
  294. Display the entire module: both code and unformatted pod documentation.
  295. This may be useful if the docs don't explain a function in the detail
  296. you need, and you'd like to inspect the code directly; perldoc will find
  297. the file for you and simply hand it off for display.
  298.  
  299. =item B<PageName|ModuleName|ProgramName>
  300.  
  301. The item you want to look up.  Nested modules (such as C<File::Basename>)
  302. are specified either as C<File::Basename> or C<File/Basename>.  You may also
  303. give a descriptive name of a page, such as C<perlfunc>. You make also give a
  304. partial or wrong-case name, such as "basename" for "File::Basename", but
  305. this will be slower, if there is more then one page with the same partial
  306. name, you will only get the first one.
  307.  
  308. =back
  309.  
  310. =head1 ENVIRONMENT
  311.  
  312. Any switches in the C<PERLDOC> environment variable will be used before the 
  313. command line arguments.  C<perldoc> also searches directories
  314. specified by the C<PERL5LIB> (or C<PERLLIB> if C<PERL5LIB> is not
  315. defined) and C<PATH> environment variables.
  316. (The latter is so that embedded pods for executables, such as
  317. C<perldoc> itself, are available.)
  318.  
  319. =head1 AUTHOR
  320.  
  321. Kenneth Albanowski <kjahds@kjahds.com>
  322.  
  323. Minor updates by Andy Dougherty <doughera@lafcol.lafayette.edu>
  324.  
  325. =head1 SEE ALSO
  326.  
  327. =head1 DIAGNOSTICS
  328.  
  329. =cut
  330.  
  331. #
  332. # Version 1.11: Tue Dec 26 09:54:33 EST 1995
  333. #       Kenneth Albanowski <kjahds@kjahds.com>
  334. #   -added Charles Bailey's further VMS patches, and -u switch
  335. #   -added -t switch, with pod2text support
  336. # Version 1.10: Thu Nov  9 07:23:47 EST 1995
  337. #        Kenneth Albanowski <kjahds@kjahds.com>
  338. #    -added VMS support
  339. #    -added better error recognition (on no found pages, just exit. On
  340. #     missing nroff/pod2man, just display raw pod.)
  341. #    -added recursive/case-insensitive matching (thanks, Andreas). This
  342. #     slows things down a bit, unfortunately. Give a precise name, and
  343. #     it'll run faster.
  344. #
  345. # Version 1.01:    Tue May 30 14:47:34 EDT 1995
  346. #        Andy Dougherty  <doughera@lafcol.lafayette.edu>
  347. #   -added pod documentation.
  348. #   -added PATH searching.
  349. #   -added searching pod/ subdirectory (mainly to pick up perlfunc.pod
  350. #    and friends.
  351. #
  352. #
  353. # TODO:
  354. #
  355. #    Cache directories read during sloppy match
  356.